//
//  GoodsPicView.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/25.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "GoodsPicView.h"

@interface GoodsPicView () <SDCycleScrollViewDelegate>

@property (strong, nonatomic) UIView *goodsPicView;
@property (strong, nonatomic) SDCycleScrollView *goodsPicCycleScrollView;
@property (strong, nonatomic) UIView *pageView;
@property (strong, nonatomic) UILabel *pageL;

@end

@implementation GoodsPicView

- (instancetype)init {
    
    if (self = [super init]) {
        
        [self addSubview:self.goodsPicView];
        [self.goodsPicView addSubview:self.goodsPicCycleScrollView];
        [self.goodsPicView addSubview:self.pageView];
        [self.pageView addSubview:self.pageL];
        
    }
    
    return self;
}

- (void)setImageArr:(NSArray *)imageArr {
    
    _imageArr = imageArr;
    
    self.goodsPicCycleScrollView.imageURLStringsGroup = imageArr;
    
    NSString *str = [NSString stringWithFormat:@"1 / %zd", imageArr.count];
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attributeStr addAttribute:NSFontAttributeName value:Font(24) range:NSMakeRange(0, 1)];
    [attributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(1, str.length - 1)];
    self.pageL.attributedText = attributeStr;
    
    
    __weak typeof(self) weakSelf = self;
    
    self.goodsPicCycleScrollView.itemDidScrollOperationBlock = ^(NSInteger currentIndex) {
        
        NSLog(@"商品详情页轮播图滑动到第%zd张", currentIndex + 1);
        
        NSString *str = [NSString stringWithFormat:@"%zd / %zd", currentIndex + 1, imageArr.count];
        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str];
        [attributeStr addAttribute:NSFontAttributeName value:Font(24) range:NSMakeRange(0, 1)];
        [attributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(1, str.length - 1)];
        weakSelf.pageL.attributedText = attributeStr;
        
    };
    
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    [self.goodsPicView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.top.mas_equalTo(weakSelf.mas_top);
        make.height.mas_equalTo(jisuan(750));
        
    }];
    [self.goodsPicCycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.goodsPicView.mas_left);
        make.right.mas_equalTo(weakSelf.goodsPicView.mas_right);
        make.top.mas_equalTo(weakSelf.goodsPicView.mas_top);
        make.bottom.mas_equalTo(weakSelf.goodsPicView.mas_bottom);
        
    }];
    
    [self.pageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.goodsPicView.mas_right).with.offset(-jisuan(20));
        make.bottom.mas_equalTo(weakSelf.goodsPicView.mas_bottom).with.offset(-jisuan(10));
        make.width.mas_equalTo(jisuan(90));
        make.height.mas_equalTo(jisuan(40));
    }];
    [self.pageL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.pageView.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.pageView.mas_centerY);
    }];

}

#pragma mark -lazy
- (UIView *)goodsPicView {
    
    if (!_goodsPicView) {
        
        _goodsPicView = [[UIView alloc] init];
        _goodsPicView.backgroundColor = WhiteColor;
    }
    
    return _goodsPicView;
    
}

- (SDCycleScrollView *)goodsPicCycleScrollView {
    
    if (!_goodsPicCycleScrollView) {
        
//        _goodsPicCycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero imageURLStringsGroup:nil];
        _goodsPicCycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:nil placeholderImage:[UIImage imageNamed:@"supportContentGoodsBG"]];
        
        
        _goodsPicCycleScrollView.backgroundColor = WhiteColor;
        
        _goodsPicCycleScrollView.autoScroll = NO;
        _goodsPicCycleScrollView.showPageControl = NO;
        _goodsPicCycleScrollView.infiniteLoop = NO;

    }
    
    return _goodsPicCycleScrollView;
}

- (UIView *)pageView {
    
    if (!_pageView) {
        
        _pageView = [[UIView alloc] init];
        _pageView.backgroundColor = Color999999;
        _pageView.layer.cornerRadius = jisuan(20);
        _pageView.layer.masksToBounds = YES;
        
    }
    
    return _pageView;
}

- (UILabel *)pageL {
    
    if (!_pageL) {
        
        _pageL = [[UILabel alloc] init];
        //        _pageL.font = Font(24);
        _pageL.textColor = WhiteColor;
        
//        NSString *str = @"1 / 5";
//        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str];
//        [attributeStr addAttribute:NSFontAttributeName value:Font(24) range:NSMakeRange(0, 1)];
//        [attributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(1, str.length - 1)];
//        _pageL.attributedText = attributeStr;
        
    }
    
    return _pageL;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
